package br.com.californiahotel.client;

import br.com.californiahotel.client.viewControler.LoginController;
import br.com.californiahotel.client.viewControler.LoginView;
import br.com.californiahotel.client.viewControler.MainController;
import br.com.californiahotel.client.viewControler.MainView;
import br.com.californiahotel.client.viewControler.apartamento.ApartamentoController;
import br.com.californiahotel.client.viewControler.apartamento.ApartamentoFormController;
import br.com.californiahotel.client.viewControler.apartamento.ApartamentoFormView;
import br.com.californiahotel.client.viewControler.apartamento.ApartamentoView;
import br.com.californiahotel.client.viewControler.hospede.HospedeController;
import br.com.californiahotel.client.viewControler.hospede.HospedeFormController;
import br.com.californiahotel.client.viewControler.hospede.HospedeFormView;
import br.com.californiahotel.client.viewControler.hospede.HospedeView;
import br.com.californiahotel.client.viewControler.locacao.LocacaoController;
import br.com.californiahotel.client.viewControler.locacao.LocacaoFormController;
import br.com.californiahotel.client.viewControler.locacao.LocacaoFormView;
import br.com.californiahotel.client.viewControler.locacao.LocacaoView;
import br.com.californiahotel.client.viewControler.reserva.ReservaController;
import br.com.californiahotel.client.viewControler.reserva.ReservaFormController;
import br.com.californiahotel.client.viewControler.reserva.ReservaFormView;
import br.com.californiahotel.client.viewControler.reserva.ReservaView;
import br.com.californiahotel.client.viewControler.servico.ServicoController;
import br.com.californiahotel.client.viewControler.servico.ServicoFormController;
import br.com.californiahotel.client.viewControler.servico.ServicoFormView;
import br.com.californiahotel.client.viewControler.servico.ServicoView;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.layout.VLayout;

public class CaliforniaHotel implements EntryPoint {

	public enum History {
		LOGIN("login"), MAIN("main"), APARTAMENTOS("apartamentos"), SERVICOS(
				"servicos"), NOVO_SERVICO("novoServico"), CLEAN_MAIN(""), NOVO_APARTAMENTO(
				"novoApartamento"), HOSPEDES("hospedes"), NOVO_HOSPEDE(
				"novoHospede"), RESERVAR_APTO("reserva"), NOVA_RESERVA(
				"novaReserva"), ALUGAR_APTO("alugarApartamento"), NOVA_LOCACAO(
				"novaLocacao"), DESPESAS("despesas"), NOVA_DESPESA(
				"novaDespesa");
		private final String token;

		private History(String token) {
			this.token = token;
		}

		public String getToken() {
			return token;
		}
	}

	private static Canvas mainView;

	@Override
	public void onModuleLoad() {
		com.google.gwt.user.client.History
				.addValueChangeHandler(new ValueChangeHandler<String>() {

					@Override
					public void onValueChange(ValueChangeEvent<String> event) {
						if (event.getValue().equals(History.LOGIN.getToken())
								|| event.getValue().equals(
										History.MAIN.getToken())) {
							if (mainView != null) {
								mainView.destroy();
							}
							if (event.getValue().equals(
									History.LOGIN.getToken())) {
								mainView = new LoginController(new LoginView())
										.getView();
							} else {
								mainView = new MainController(new MainView())
										.getView();
							}
							if (mainView != null) {
								mainView.draw();
							}
						} else if (event.getValue().equals(
								History.APARTAMENTOS.getToken())) {
							changeMainContent(new ApartamentoController(
									new ApartamentoView()).getView());
						} else if (event.getValue().equals(
								History.SERVICOS.getToken())) {
							changeMainContent(new ServicoController(
									new ServicoView()).getView());
						} else if (event.getValue().equals(
								History.HOSPEDES.getToken())) {
							changeMainContent(new HospedeController(
									new HospedeView()).getView());
						} else if (event.getValue().equals(
								History.NOVO_SERVICO.getToken())) {
							changeMainContent(new ServicoFormController(
									new ServicoFormView()).getView());
						} else if (event.getValue().equals(
								History.NOVO_APARTAMENTO.getToken())) {
							changeMainContent(new ApartamentoFormController(
									new ApartamentoFormView()).getView());
						} else if (event.getValue().equals(
								History.NOVO_HOSPEDE.getToken())) {
							changeMainContent(new HospedeFormController(
									new HospedeFormView()).getView());
						} else if (event.getValue().equals(
								History.CLEAN_MAIN.getToken())) {
							changeMainContent(new VLayout());
						} else if (event.getValue().equals(
								History.RESERVAR_APTO.getToken())) {
							changeMainContent(new ReservaController(
									new ReservaView()).getView());
						} else if (event.getValue().equals(
								History.NOVA_RESERVA.getToken())) {
							changeMainContent(new ReservaFormController(
									new ReservaFormView()).getView());
						} else if (event.getValue().equals(
								History.ALUGAR_APTO.getToken())) {
							changeMainContent(new LocacaoController(
									new LocacaoView()).getView());
						} else if (event.getValue().equals(
								History.NOVA_LOCACAO.getToken())) {
							changeMainContent(new LocacaoFormController(
									new LocacaoFormView()).getView());
						} else {
							mainView.destroy();
							SC.say("Pagina invalida");
							changeHistory(History.MAIN);
						}
					}

				});
		changeHistory(History.LOGIN);
		com.google.gwt.user.client.History.fireCurrentHistoryState();
	}

	public static void changeHistory(History newPage) {
		if (com.google.gwt.user.client.History.getToken().equals(
				newPage.getToken())) {
			com.google.gwt.user.client.History.newItem(History.CLEAN_MAIN
					.getToken());
		}
		com.google.gwt.user.client.History.newItem(newPage.getToken());
	}

	public static final void changeMainContent(Canvas canvas) {
		((MainView) mainView).changeContent(canvas);
	}
}
